<?php

namespace app\common\model\mysql;

use think\Model;

class AllotLibrary extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'p_allot_library';

    /**
     * 新增店内调拨单
     * @param $data
     * @return mixed
     * @throws \think\Exception
     */
    public function addAllotLibraryData($data){
        $this->save($data);
        return $this->id;
    }

    /**
     * 根据where条件获取店内调拨数据
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllotLibraryFind(array $where = [], string $field = "*"){
        $result = $this->where($where)->field($field)->find();
        return $result ? $result->toArray() : [];
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function saveAllotLibraryData($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            "id"=>$id,
        ];
        return $this->where($where)->save($data);
    }

    /**
     * 根据where条件获取配送单分页数据
     * @param $where
     * @param $limit
     * @param $page
     * @param $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getNormalAllotLibraryList($where,$limit,$page,$field){
        $order = [
            "id"=>"desc"
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result->toArray();
    }
}
